<?php
/**
 * Class that operate on table 'event_theaters'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class EventTheatersMySqlDAO implements EventTheatersDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return EventTheatersMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM event_theaters WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM event_theaters';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM event_theaters ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param eventTheater primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM event_theaters WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param EventTheatersMySql eventTheater
 	 */
	public function insert($eventTheater){
		$sql = 'INSERT INTO event_theaters (event_id, heading, address, ticket_cost, currency_type, created_by, create_date, updated_by, updated_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($eventTheater->eventId);
		$sqlQuery->set($eventTheater->heading);
		$sqlQuery->set($eventTheater->address);
		$sqlQuery->setNumber($eventTheater->ticketCost);
		$sqlQuery->setNumber($eventTheater->currencyType);
		$sqlQuery->setNumber($eventTheater->createdBy);
		$sqlQuery->set($eventTheater->createDate);
		$sqlQuery->setNumber($eventTheater->updatedBy);
		$sqlQuery->set($eventTheater->updated_date);
		$sqlQuery->setNumber($eventTheater->status);

		$id = $this->executeInsert($sqlQuery);	
		$eventTheater->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param EventTheatersMySql eventTheater
 	 */
	public function update($eventTheater){
		$sql = 'UPDATE event_theaters SET event_id = ?, heading = ?, address = ?, ticket_cost = ?, currency_type = ?, created_by = ?, create_date = ?, updated_by = ?, updated_date = ?, status = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($eventTheater->eventId);
		$sqlQuery->set($eventTheater->heading);
		$sqlQuery->set($eventTheater->address);
		$sqlQuery->setNumber($eventTheater->ticketCost);
		$sqlQuery->setNumber($eventTheater->currencyType);
		$sqlQuery->setNumber($eventTheater->createdBy);
		$sqlQuery->set($eventTheater->createDate);
		$sqlQuery->setNumber($eventTheater->updatedBy);
		$sqlQuery->set($eventTheater->updated_date);
		$sqlQuery->setNumber($eventTheater->status);

		$sqlQuery->setNumber($eventTheater->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM event_theaters';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByEventId($value){
		$sql = 'SELECT * FROM event_theaters WHERE event_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByHeading($value){
		$sql = 'SELECT * FROM event_theaters WHERE heading = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress($value){
		$sql = 'SELECT * FROM event_theaters WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTicketCost($value){
		$sql = 'SELECT * FROM event_theaters WHERE ticket_cost = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCurrencyType($value){
		$sql = 'SELECT * FROM event_theaters WHERE currency_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedBy($value){
		$sql = 'SELECT * FROM event_theaters WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreateDate($value){
		$sql = 'SELECT * FROM event_theaters WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedBy($value){
		$sql = 'SELECT * FROM event_theaters WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM event_theaters WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM event_theaters WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByEventId($value){
		$sql = 'DELETE FROM event_theaters WHERE event_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByHeading($value){
		$sql = 'DELETE FROM event_theaters WHERE heading = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress($value){
		$sql = 'DELETE FROM event_theaters WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTicketCost($value){
		$sql = 'DELETE FROM event_theaters WHERE ticket_cost = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCurrencyType($value){
		$sql = 'DELETE FROM event_theaters WHERE currency_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedBy($value){
		$sql = 'DELETE FROM event_theaters WHERE created_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreateDate($value){
		$sql = 'DELETE FROM event_theaters WHERE create_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedBy($value){
		$sql = 'DELETE FROM event_theaters WHERE updated_by = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM event_theaters WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM event_theaters WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return EventTheatersMySql 
	 */
	protected function readRow($row){
		$eventTheater = new EventTheater();
		
		$eventTheater->id = $row['id'];
		$eventTheater->eventId = $row['event_id'];
		$eventTheater->heading = $row['heading'];
		$eventTheater->address = $row['address'];
		$eventTheater->ticketCost = $row['ticket_cost'];
		$eventTheater->currencyType = $row['currency_type'];
		$eventTheater->createdBy = $row['created_by'];
		$eventTheater->createDate = $row['create_date'];
		$eventTheater->updatedBy = $row['updated_by'];
		$eventTheater->updated_date = $row['updated_date'];
		$eventTheater->status = $row['status'];

		return $eventTheater;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return EventTheatersMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>